﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Seagull.BarTender.Print;
using Luna小店.UC.套号管理.套号吊牌.model;
using Luna小店.UC.款式异常;
using Luna小店.DTO;
using DevExpress.XtraGrid.Demos.util;
using Luna小店.service;
using Luna小店.UC.套号管理.套号吊牌.service;
using System.IO;

namespace Luna小店.UC.套号管理.套号吊牌
{
    public partial class Form套号吊牌 : DevExpress.XtraEditors.XtraForm
    {
        private Double price { get; set; }
        private String sessionId;
        private String ORDER_ID;
        private DTO套号吊牌一览 DTO套号吊牌一览 = new DTO套号吊牌一览();
        private StyleCategoryDTO styleCategoryDTO { get; set; }
        private 门店下单选款式Model model;
        private List<DTO条码> list = new List<DTO条码>();
        public Form套号吊牌(
            String ORDER_ID,
            String sessionId,
            门店下单选款式Model model,
            StyleCategoryDTO styleCategoryDTO
            )
        {
            InitializeComponent();
            this.sessionId = sessionId;
            this.ORDER_ID = ORDER_ID;
            this.DTO套号吊牌一览 = 套号吊牌Service.get套号吊牌打印内容ByOrderId(this.ORDER_ID,this.sessionId);
            this.model = model;
            this.styleCategoryDTO = styleCategoryDTO;
            this.textBox款名.Text = this.DTO套号吊牌一览.styleNameCn.Split(' ')[1];
            this.list = new 套号吊牌Model().build列表(this.DTO套号吊牌一览.inventoryNos);
            this.gridControl吊牌扫码.DataSource = this.list;
        }

        private void simpleButton打印_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox价格.Text))
            {
                MessageBox.Show("请填写价格");
                return;
            }
            else
            {
                this.price = Convert.ToDouble(this.textBox价格.Text);
                this.根据类型获取模板();
            }
        }
        private void 根据类型获取模板()
        {
            //根据类型获取不同模板
            DTO套号吊牌模板 dto = 套号吊牌Service.get根据类型获取不同模板(this.styleCategoryDTO.id);
            String fileName = Path.GetFileName(dto.filePath);
            //根据相对路径判断模板是否存在
            //不存在则下载进行打印
            if (!System.IO.File.Exists(@"tag\" + fileName))
            {
                PictureService.DownloadPicture(URL.图片url + "/print_template/" + dto.filePath, @"tag\" + fileName, -1);
            }
            else  //模板存在时根据md5判断是否匹配,匹配直接打印
            {
                if (!FileService.GetMD5HashFromFile(@"tag\" + fileName).Equals(dto.md5))
                {
                    PictureService.DownloadPicture(URL.图片url + "/print_template/" + dto.filePath, @"tag\" + fileName, -1);
                }
            }
            foreach (String inventoryNo in this.DTO套号吊牌一览.inventoryNos)
            {
                this.print套号吊牌(@"tag\" + fileName, inventoryNo);
            }
        }

        private void print套号吊牌(String file,String inventoryNo)
        {
            String str = System.AppDomain.CurrentDomain.BaseDirectory;
            Engine btEngine = new Engine();
            btEngine.Start();
            LabelFormatDocument btFormat = btEngine.Documents.Open(str+file);
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 2;
            //对BTW模版相应字段进行赋值 
            btFormat.SubStrings["customerName"].Value = this.model.订单基本信息.收货人姓名;
            btFormat.SubStrings["styleNo"].Value = this.DTO套号吊牌一览.styleNameCn.Split(' ')[0];
            btFormat.SubStrings["styleName"].Value = this.textBox款名.Text;
            btFormat.SubStrings["fabricNumber"].Value = this.DTO套号吊牌一览.materialCode;
            btFormat.SubStrings["fabricComposition"].Value = this.DTO套号吊牌一览.materialComposition;
            btFormat.SubStrings["price"].Value = "¥" + this.price.ToString();
            btFormat.SubStrings["size"].Value = this.DTO套号吊牌一览.styleSizeCd;
            btFormat.SubStrings["inventoryNo"].Value = inventoryNo;

            //不同款式类型吊牌打印内容不同
            套号吊牌Model 套号吊牌Model = new 套号吊牌Model().buildDTO套号吊牌s(this.DTO套号吊牌一览.measureItemDTOS);
            foreach (DTO套号吊牌 dTO in 套号吊牌Model.DTO套号吊牌s)
            {
                try
                {
                    btFormat.SubStrings[dTO.吊牌项目].Value = dTO.成衣值;
                }
                catch { }
            }
            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Result nResult1 = btFormat.Print("套号吊牌" + this.model.订单基本信息.ORDER_NO, waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }

        private void textBox价格_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键和负号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox价格.Text = "";
                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (textBox价格.Text != "")
                {
                    //MessageBox.Show("请输入正确的数字");
                    this.textBox价格.Text = "";
                    e.Handled = true;
                }
            }
            //小数点只能输入一次  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox价格.Text = "";
                e.Handled = true;
            }
            //第一位不能为小数点  
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox价格.Text = "";
                e.Handled = true;
            }
            //第一位是0，第二位必须为小数点  
            if (e.KeyChar != (char)('.') && ((TextBox)sender).Text == "0")
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox价格.Text = "";
                e.Handled = true;
            }
            //第一位是负号，第二位不能为小数点  
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                //MessageBox.Show("请输入正确的数字");
                this.textBox价格.Text = "";
                e.Handled = true;
            }
        }

        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                //扫码预入库
                套号吊牌Service.吊牌扫码预入库(this.textBox扫码.Text);
                //预入库完成改表状态
                this.list = new 套号吊牌Model().build条码状态(this.list,this.textBox扫码.Text);
                this.gridControl吊牌扫码.DataSource = this.list;
                this.gridView吊牌扫码.RefreshData();
                this.textBox扫码.Clear();
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DTO返回 DTO返回 = 套号吊牌Service.套号确认入库(this.sessionId);
            if (!DTO返回.errorCode.Equals("200"))
            {
                MessageBox.Show(DTO返回.errorMessage);
            }
            this.Close();
        }

        private void Form套号吊牌_FormClosing(object sender, FormClosingEventArgs e)
        {
            套号吊牌Service.清理session(this.sessionId);
        }
    }
}